package com.dc.java8._4_forkJoin;

import org.junit.Test;

import java.util.concurrent.ForkJoinPool;
import java.util.stream.LongStream;

/**
 * Description: ForkJoin 测试类
 * @author: gdc
 * @date: 2021/4/10
 * @version 1.0
 */
public class ForkJoinTest {
	
	@Test
	public void test1() {
		long start = System.currentTimeMillis();
		
		ForkJoinPool pool = new ForkJoinPool();
		ForkJoinCalculate task = new ForkJoinCalculate(0L, 100000000000L);
		Long sum = pool.invoke(task);
		System.out.println(sum);
		long end = System.currentTimeMillis();
		System.out.println("耗费的时间为:" + (end - start));		// 33738  932356074711512064
	}
	
	@Test
	public void test2() {
		long start = System.currentTimeMillis();
		long sum = 0L;
		for (int i = 0; i < 100000000000L; i++) {
			sum += i;
		}
		System.out.println(sum);
		long end = System.currentTimeMillis();
		System.out.println("耗费的时间为:" + (end - start));		// 时间太久
	}
	
	@Test
	public void test3() {
		long start = System.currentTimeMillis();
		long sum = LongStream.rangeClosed(0L, 100000000000L)
					.parallel()
					.sum();
		System.out.println(sum);
		long end = System.currentTimeMillis();
		System.out.println("耗费的时间为:" + (end - start));		//27228  	932356074711512064
	}
	
}
